package google.news.worker;

import db.dao.BaseDao;
import domain.result.Result7;
import domain.task.Task7;
import google.news.HttpDownLoader;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2017/1/29.
 */
public class Worker7 extends Thread {

    Pattern resultPattern = Pattern.compile("<div id=\\\"resultStats\\\">.+?([\\d,]{1,}).+?<nobr>", Pattern.DOTALL);
    Pattern companyPattern = Pattern.compile("\\[HOST\\](.+?)&");
    private BaseDao baseDao = new BaseDao();
    private HttpDownLoader downLoader = new HttpDownLoader();

    public static void main(String[] args) {
        new Worker7().start();
    }

    @Override
    public void run() {
        work();
    }

    public void work() {
        setName("Task7 worker");
        List<Task7> task7s = baseDao.find("from Task7 where done=0");
        int size = task7s.size();
        int counter = 1;
        System.out.println("load " + size + " task7s");
        for (Task7 task7 : task7s) {
            System.out.println(getName() + "====" + new Date().toLocaleString() + " process task7, " + counter++ + "/" + size);
            int result = 0;
            String url = task7.getUrl().replace("[HOST]", Mirror.getMirror());
            System.out.println("process " + url);
            // 找不到包含所有搜索字词的结果
            String content = downLoader.download(url, "localProxy");
            Matcher matcher = resultPattern.matcher(content);
            if (matcher.find()) {
                String str = matcher.group(1);
                str = str.replaceAll(",", "");
                result = Integer.parseInt(str);
            } else if (content.contains("未搜到与") && content.contains("相关的新闻")) {
                result = 0;
            } else {
                System.err.println("被封禁了");
//                System.out.println(content);
                continue;
            }
            String company = "";
            matcher = companyPattern.matcher(task7.getUrl());
            if (matcher.find()) {
                company = matcher.group(1);
            }
            System.out.println(url + " got : " + result);
            Result7 result7 = new Result7();
            result7.setTaskId(task7.getId());
            result7.setNewsCount(result);
            result7.setPeriod(task7.getPeriod());
            result7.setCompanyName(company);
            baseDao.create(result7);
            task7.setDone(1);
            baseDao.update(task7);
        }
    }
}
